<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>



    <script>
        // 猴子排序
        let arr= [ 2 , 45 ,78 ,92 , 34 , 35]
        let arr1= []   // 
        let arr2 = []  
        let index = 0 ;
        function monkey(){  
            for( let i = 0 ; i < arr.length ; i++ ){
                if(arr.length === arr1.length){return}
                let n = parseInt(Math.random()*arr.length)
                arr1.includes(n) ? monkey() : arr1.push(n)
            }
        }
        monkey()

        function fn(){
            index++
            for(let i = 0 ; i < arr.length; i++ ){
                arr2[i] = arr[arr1[i]]
            }
            for(let i = 0 ; i < arr2.length -1 ; i++){
               
                if( arr2[i] > arr2[i+1]){
                    arr1 = []
                    monkey()
                    fn()
                }
            }
            return
        }
        fn()
        console.log(arr2)
        console.log(index)
    </script>
    
</body>
</html>